package io.renren.modules.sys.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.renren.common.dao.LambdaQueryWrapperX;
import io.renren.common.page.PageData;

import io.renren.modules.sys.dao.CaseMapper;
import io.renren.modules.sys.dto.CaseCreateDTO;
import io.renren.modules.sys.entity.CaseEntity;
import io.renren.modules.sys.service.CaseService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

/**
 * @author jinpf
 * @date 2025-03-13
 */
@Service
public class CaseServiceImpl implements CaseService {

    @Resource
    private CaseMapper caseMapper;

    @Override
    public void createCase(CaseCreateDTO caseCreateDTO) {
        CaseEntity caseEntity = BeanUtil.copyProperties(caseCreateDTO, CaseEntity.class);
        caseMapper.insert(caseEntity);
    }

    @Override
    public void deleteCase(Integer id) {
        caseMapper.deleteById(id);
    }

    @Override
    public void updateCase(CaseCreateDTO caseCreateDTO) {
        CaseEntity caseEntity = BeanUtil.copyProperties(caseCreateDTO, CaseEntity.class);
        caseMapper.updateById(caseEntity);
    }

    @Override
    public PageData getPage(CaseCreateDTO caseCreateDTO) {
        Page page = new Page(caseCreateDTO.getPageNum(), caseCreateDTO.getPageSize());
        LambdaQueryWrapperX<CaseEntity> queryWrapperX = new LambdaQueryWrapperX<>();
        queryWrapperX.likeIfPresent(CaseEntity::getTitle,caseCreateDTO.getTitle());
        queryWrapperX.likeIfPresent(CaseEntity::getBg,caseCreateDTO.getBg());
        queryWrapperX.likeIfPresent(CaseEntity::getYg,caseCreateDTO.getYg());
        queryWrapperX.likeIfPresent(CaseEntity::getFy,caseCreateDTO.getFy());
        Page pageResult = caseMapper.selectPage(page, queryWrapperX);
        return new PageData(pageResult.getRecords(), pageResult.getTotal());
    }
}
